package com.dpj.nio.n3.Multiply;

import com.dpj.nio.c1.ByteBufferUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class MultiplyThreadServer {

    public static void main(String[] args) throws IOException {
        Thread.currentThread().setName("boss");
        ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.configureBlocking(false);
        Selector boss = Selector.open();
        SelectionKey bossKey = ssc.register(boss, 0, null);
        bossKey.interestOps(SelectionKey.OP_ACCEPT);
        ssc.bind(new InetSocketAddress(8080));
        //1 创建固定数量的worker 并初始化   Runtime.getRuntime().availableProcessors()--是cpu核心数
        Worker[] workers=new Worker[Runtime.getRuntime().availableProcessors()];
        for (int i = 0; i < workers.length ; i++) {
            workers[i]=new Worker("worker-"+i);
        }
        AtomicInteger index=new AtomicInteger();

        while (true) {
            boss.select();
            Iterator<SelectionKey> iterator = boss.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                iterator.remove();
                if (key.isAcceptable()) {
                    SocketChannel sc = ssc.accept();
                    sc.configureBlocking(false);
                    log.debug("connected..{}",sc.getRemoteAddress());
                    //2.关联selector
                    log.debug("before register",sc.getRemoteAddress());
                    //round robin
                    workers[index.getAndIncrement()% workers.length].register(sc);  //boss 调用 ，初始化register ,启动 worker-

                    log.debug("after register",sc.getRemoteAddress());

                }
            }
        }
    }

    static class Worker implements Runnable {
        private Thread thread;
        private Selector selector;
        private String name;
        private volatile boolean start = false; //还未初始化

        private ConcurrentLinkedDeque<Runnable> queue=new ConcurrentLinkedDeque<>();

        public Worker(String name) {
            this.name = name;
        }

        //初始化线程和selector
        public void register(SocketChannel sc) throws IOException {
            if (!start) {
                thread.start();
                selector = Selector.open();
                start = true;
            }
            //相对列中加入了任务，但这个任务并没有立刻执行 boss
            queue.add(()->{
                try {
                    sc.register(selector,SelectionKey.OP_READ,null); //boss
                } catch (ClosedChannelException e) {
                    e.printStackTrace();
                }
            });
            selector.wakeup();//唤醒select方法


        }

        @SneakyThrows
        @Override
        public void run() {
            while (true) {
                selector.select(); //worker-0  阻塞 wakeup
                Runnable task = queue.poll();
                if(task!=null){
                    task.run(); //执行了 sc.register(selector,SelectionKey.OP_READ,null);
                }
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while (iterator.hasNext()) {
                    SelectionKey key = iterator.next();
                    iterator.remove();
                    if (key.isReadable()) {
                        ByteBuffer buffer = ByteBuffer.allocateDirect(16);
                        SocketChannel channel = (SocketChannel) key.channel();
                        log.debug("read..{}",channel.getRemoteAddress());
                        channel.read(buffer);
                        buffer.flip();
                        ByteBufferUtil.debugAll(buffer);
                    }
                }
            }
        }
    }
}
